package com.dg.gaming.client.gui;

import nanoxml.XMLElement;
import com.dg.gaming.client.ClientConnectionThread;
import com.dg.gaming.api.common.Game;
import com.dg.gaming.api.common.TransmissionException;
import com.dg.gaming.api.common.msg.*;
import com.dg.gaming.api.common.util.GameProperties;
import com.dg.gaming.api.common.util.DGLabels;
import java.net.Socket;
import com.dg.gaming.client.TableConnection;
import com.dg.gaming.api.common.*;


public class TableConnectionPanel extends ConnectionPanel {
    /** Link the GUI. */
    private DGGameTable client;

    /** Link to the client connection thread for the game. */
    private TableConnection conn = null;

    /**
     * Constructor for an application which doesn't take a
     * username / password.
     *
     * @param client   Link to frame / applet.
     */
    public TableConnectionPanel(DGGameTable client) {
        super();

        this.client = client;
    }

    /**
     * Silent connect which takes a server, port, username and password.
     *
     * @param client    Link to frame / applet.
     * @param server    Name of the server.
     * @param port      Server port.
     * @param username  Username of person trying to connect.
     * @param password  Password of the person trying to connect.
     */
    public TableConnectionPanel(DGGameTable client,
                                String server,
                                int port,
                                long username,
                                String tableNum) {
        super(server, port, username, tableNum);

        this.client = client;
    }

    /**
     * Override the connect method for handling game traffic.
     *
     * @see com.dg.gaming.client.gui.awt.ConnectionPanel#connect(java.lang.String, int, java.lang.String, java.lang.String)
     */
    protected void connect(Socket socket,
                           long username,
                           String tableNum) {
        // Create a new connection thread to handle communication
        this.conn = new TableConnection(socket, username, this);
        new Thread(conn).start(); // Start the thread

        // Create a connect message.
        MsgConnectTable commConnect = null;
//        if (password.equals("")) {
//            commConnect = new MsgGameConnect
//                          (username, GameProperties.getGameID());
//        } else {
//            commConnect = new MsgGameConnect
//                          (username, password, GameProperties.getGameID());
//        }
        commConnect = new MsgConnectTable(Long.parseLong(tableNum), username,
                                          Long.parseLong(GameProperties.
                getGameID()));
        // Send connect message to the server.
        conn.send(commConnect);

    }

    /**
     * @see com.dg.gaming.client.gui.IClient#receiveGameMessage(nanoxml.XMLElement)
     */
    public void receiveGameMessage(XMLElement message) {
        String messageType = message.getName();

        // Message could be
        //     <error> - Error message.
        //     <game>  - Game message (success)
        if (messageType.equals(Msg.ERROR)) {
            // Create comm object and create error description
            MsgError commError = new MsgError(message);
            String errorStr = DGLabels.getError(commError.getStatus());

            // Update label
            statusLabel.setText(errorStr);
        } else if (messageType.equals(Msg.TABLE)) {
            try {
                Table table = new Table(message);
                // Create game object on connection
                conn.setTable(table);
                for (Object elem : message.getChildren()) {
                    XMLElement childMessage = (XMLElement) elem;
                    if (childMessage.getName().equals(Msg.MODEL)) {
                        GameModel model = client.getGameModel();
                        if (model != null) {
                            model.setState(childMessage);
                            table.setModel(model);
                        }
                    }
                }
                // User is sucessfully connected so set game object and return object.
                client.connectOK(conn);
            } catch (TransmissionException tEx) {}
        }
    }

    public void receiveTableMessage(XMLElement message, String tableNum) {} // does nothing


}
